package com.beverages.messages.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.beverages.messages.client.CustomerMessagesClient;
import com.beverages.messages.client.MerchantIndividualClient;
import com.beverages.messages.common.Result;
import com.beverages.messages.entity.RefundReply;
import com.beverages.messages.entity.dto.RefundReplyDTO;
import com.beverages.messages.entity.dto.intermediate.RefundRequestDTO;
import com.beverages.messages.entity.intermediate.RefundRequest;
import com.beverages.messages.mapper.RefundReplyMapper;
import com.beverages.messages.service.IRefundReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/merchant/messages/reply-for-refund")
public class RefundReplyController {

    private final RestTemplate restTemplate;
    private static final String VUE_URL = "http://127.0.0.1:9091/send-message/"; //后续需要修改

    @Autowired
    public RefundReplyController(@Qualifier("restTemplateForVue") RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @Autowired
    private MerchantIndividualClient merchantIndividualClient;

    @Autowired
    private CustomerMessagesClient customerMessagesClient;

    @Resource
    private IRefundReplyService refundReplyService;

    @Resource
    private RefundReplyMapper refundReplyMapper;

    @GetMapping("/id/{replyId}")
    public Result<RefundReply> getRefundReplyById(@PathVariable("replyId") Integer replyId) {
        return Result.success(refundReplyService.getById(replyId));
    }

    @GetMapping("/{replyId}")
    public Result<RefundReplyDTO> getSingleRefundReplyDTOById(@PathVariable("replyId") Integer replyId){
        return Result.success(refundReplyService.convertRefundReplyEntityToRefundReplyDTO(refundReplyService.getById(replyId)));
    }

    @GetMapping("/page")
    public Result<IPage<RefundRequestDTO>> findRefundReplyPage(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(defaultValue = "") String orderNumber,
            @RequestParam(defaultValue = "true") Boolean whetherOnlyShowUnreplied) {
//        IPage<RefundReply> page = new Page<>(pageNum, pageSize);
//        QueryWrapper<RefundReply> queryWrapper = new QueryWrapper<>();
//
//        if (orderNumber != null && !orderNumber.trim().equals("")) {
//            queryWrapper.eq("order_number", orderNumber.trim());
//        }
//
//        IPage<RefundReply> resultPage = refundReplyService.page(page, queryWrapper);
//
//        List<RefundReplyDTO> dtoList = resultPage.getRecords().stream().map(refundReply -> {
//            return refundReplyService.convertRefundReplyEntityToRefundReplyDTO(refundReply);
//        }).collect(Collectors.toList());
//
//        IPage<RefundReplyDTO> dtoPage = new Page<>(pageNum, pageSize, resultPage.getTotal());
//        dtoPage.setRecords(dtoList);


        return Result.success(customerMessagesClient.pageShowCustomerRefundRequest(pageNum, pageSize, orderNumber, whetherOnlyShowUnreplied).getData());
    }

    @PostMapping
    public Result reply(@RequestBody RefundReply refundReply) {
        refundReply.setId(null);
        refundReply.setReplyTime(new Date());
        refundReplyMapper.insert(refundReply);
        customerMessagesClient.refundRequestIsReplied(refundReply.getRequestId(), refundReply.getId());
        RefundRequest refundRequest = customerMessagesClient.getRefundRequestById(refundReply.getRequestId()).getData();
        merchantIndividualClient.refundToCustomer(refundRequest.getFromCustomerId(), refundReply.getAmount());
        return Result.success();
    }

}